#include <QtGui/QApplication>
#include <QTextCodec>
#include <QMessageBox>
#include <QSettings>
#include <QFileInfo>
#include <QSharedMemory>
#include <QDir>

#include "SingleRun.h"
#include "MainWindow.h"
#include "MDeviceManager.h"
#include "TranslationLanguage.h"
#include "CONST.h"

using namespace Mikroakustika::MDeviceChecker;

LSingleRun* LSingleRun::instance = 0;
TranslationLanguage* TranslationLanguage::instance = 0;

QString getApplicationFileName() {
    QString appFilePath = QCoreApplication::applicationFilePath();
    QFileInfo appInfo(appFilePath);
    QString appName = appInfo.fileName();
    QCoreApplication::setApplicationName(appName);
    return appName;
}

int main(int argc, char *argv[]) {

    Q_INIT_RESOURCE(application_res);

    QApplication a(argc, argv);

    if(LSingleRun::getInstance()->isRunningNow(applicationName))
        exit(0);


    QTextCodec *codec;
//#if defined(Q_WS_WIN)
    codec = QTextCodec::codecForName("CP1251");
//#else
//    codec = QTextCodec::codecForName("UTF-8");
//#endif

    QTextCodec::setCodecForTr(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForLocale(codec);

//    QByteArray

    setlocale(LC_ALL,"Russian");

    TranslationLanguage::getInstance()->setTranslationDir(QCoreApplication::applicationDirPath()+"\\"+"locale\\");
    TranslationLanguage::getInstance()->setTranslationFile("mdevicechecker_ru");
    TranslationLanguage::getInstance()->setLocaleForApplication();
    a.installTranslator(TranslationLanguage::getInstance()->getTranslator());

    getApplicationFileName();

    //QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() + "/plugins/");

    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        QMessageBox::critical(0, QObject::tr("Systray"),
                              QObject::tr("I couldn't detect any system tray "
                                          "on this system."));
        return 1;
    }

    QApplication::setQuitOnLastWindowClosed(false);

    MainWindow w;

    return a.exec();
}
